package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dk.model.CraftRelatedProduct;
import com.dk.mapper.CraftRelatedProductMapper;
import com.dk.model.Material;
import com.dk.service.CraftRelatedProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dk.service.CraftRouteService;
import com.dk.service.MaterialService;
import com.dk.service.RelaterProductBomService;
import com.dk.vo.CraftRelatedProductVO;
import com.dk.vo.CraftRouteVO;
import com.dk.vo.RelaterProductBomVO;
import com.dongdongshop.data.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wbh
 * @since 2023-03-20
 */
@Service
public class CraftRelatedProductServiceImpl extends ServiceImpl<CraftRelatedProductMapper, CraftRelatedProduct> implements CraftRelatedProductService {
    @Resource
    private CraftRelatedProductMapper craftRelatedProductMapper;

    @Autowired
    private CraftRouteService craftRouteService;

    @Autowired
    private RelaterProductBomService relaterProductBomService;


    @Override
    public CraftRelatedProductVO getInfoList(CraftRelatedProductVO craftRelatedProductVO) {
        //中间表,其中的craftRouteId 是必传 ,产品物料编码,产品物料名称 模糊查询
        LambdaQueryWrapper<CraftRelatedProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(craftRelatedProductVO.getCraftRouteId()), CraftRelatedProduct::getCraftRouteId, craftRelatedProductVO.getCraftRouteId());
        wrapper.like(StringUtils.isNotBlank(craftRelatedProductVO.getMaterialId()), CraftRelatedProduct::getMaterialId, craftRelatedProductVO.getMaterialId());
        wrapper.like(StringUtils.isNotBlank(craftRelatedProductVO.getMaterialName()), CraftRelatedProduct::getMaterialName, craftRelatedProductVO.getMaterialName());
        List<CraftRelatedProductVO> collect = craftRelatedProductMapper.selectList(wrapper).stream().map(craftRelatedProduct -> {
            CraftRelatedProductVO relatedProduct = new CraftRelatedProductVO();
            BeanUtils.copyProperties(craftRelatedProduct, relatedProduct);
            return relatedProduct;
        }).collect(Collectors.toList());
        craftRelatedProductVO.setList(collect);
        //查询工艺线路表
        CraftRouteVO infoCraftRouteById = craftRouteService.getInfoCraftRouteById(craftRelatedProductVO.getCraftRouteId());
        craftRelatedProductVO.setCraftRouteVO(infoCraftRouteById);
        return craftRelatedProductVO;
    }

    @Override
    public Result insertInfoCraftRelatedProduct(CraftRelatedProductVO craftRelatedProductVO) {
        //增加中间表信息,先判断此产品是否已经定义了工艺流程'
        LambdaQueryWrapper<CraftRelatedProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(craftRelatedProductVO.getMaterialId()), CraftRelatedProduct::getMaterialId, craftRelatedProductVO.getMaterialId());
        CraftRelatedProduct craftRelatedProduct1 = craftRelatedProductMapper.selectOne(wrapper);
        String materialFlag = craftRelatedProduct1.getMaterialFlag();
        if (StringUtils.equals(materialFlag,"Y")){
            return Result.FAIL();
        }
        //如果没有定义,再去定义
        CraftRelatedProduct craftRelatedProduct = new CraftRelatedProduct();
        BeanUtils.copyProperties(craftRelatedProductVO,craftRelatedProduct);
        craftRelatedProductMapper.insert(craftRelatedProduct);
        return Result.OK();
    }

    @Override
    public CraftRelatedProductVO getInfoCraftRelatedProductById(Integer id) {
        //先回显产品物料的回显
        CraftRelatedProduct craftRelatedProduct = craftRelatedProductMapper.selectById(id);
        CraftRelatedProductVO craftRelatedProductVO = new CraftRelatedProductVO();
        BeanUtils.copyProperties(craftRelatedProduct,craftRelatedProductVO);
        //在回显相关的bom表的回显,根据id去查
        List<RelaterProductBomVO> bomListById = relaterProductBomService.getBomListById(id);
        craftRelatedProductVO.setBomList(bomListById);
        return craftRelatedProductVO;
    }

    @Override
    public Result updateInfoCraftRelatedProductById(CraftRelatedProductVO craftRelatedProductVO) {
        //修改中间表信息,先判断此产品是否已经定义了工艺流程'
        LambdaQueryWrapper<CraftRelatedProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(craftRelatedProductVO.getMaterialId()), CraftRelatedProduct::getMaterialId, craftRelatedProductVO.getMaterialId());
        CraftRelatedProduct craftRelatedProduct1 = craftRelatedProductMapper.selectOne(wrapper);
        String materialFlag = craftRelatedProduct1.getMaterialFlag();
        //定义了就是修改失败
        if (StringUtils.equals(materialFlag,"Y")){
            return Result.FAIL();
        }
        //如果没有定义,再去定义
        CraftRelatedProduct craftRelatedProduct = new CraftRelatedProduct();
        BeanUtils.copyProperties(craftRelatedProductVO,craftRelatedProduct);
        craftRelatedProductMapper.updateById(craftRelatedProduct);
        return Result.OK();
    }


    @Override
    public void deleteInfoCraftRelatedProductById(Integer id) {
        craftRelatedProductMapper.deleteById(id);
        relaterProductBomService.deleteInfoRelaterProductByRelatedId(id);
    }

    @Override
    public void deleteInfoCraftRelatedCraftRouteId(String craftRouteId) {
        LambdaQueryWrapper<CraftRelatedProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CraftRelatedProduct::getCraftRouteId,craftRouteId);
        List<CraftRelatedProduct> craftRelatedProducts = craftRelatedProductMapper.selectList(wrapper);
        craftRelatedProductMapper.delete(wrapper);
        //再次删除 bom表中id
        List<Integer> list = new ArrayList<>();
        craftRelatedProducts.stream().forEach(craftRelatedProduct -> list.add(craftRelatedProduct.getId()));
        relaterProductBomService.deleteInfoRelaterProductBomByIds(list);
    }

    @Override
    public CraftRelatedProduct getInfoCraftRelatedProductByMaterialId(String materialId) {
        LambdaQueryWrapper<CraftRelatedProduct> wrapper = new LambdaQueryWrapper();
        wrapper.eq(CraftRelatedProduct::getMaterialId,materialId);
        CraftRelatedProduct craftRelatedProduct = craftRelatedProductMapper.selectOne(wrapper);
        return craftRelatedProduct;
    }
}
